package com.xdjk.networkdemo;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.orhanobut.logger.Logger;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.xdjk.networkdemo.activity.ScrollingActivity;
import com.xdjk.networkdemo.activity.SlideActivity;
import com.xdjk.networkdemo.adapter.ListAdapter;
import com.xdjk.networkdemo.base.BaseActivity;
import com.xdjk.networkdemo.entity.Principal;
import com.xdjk.networkdemo.listener.OnItemClickLitener;
import com.xdjk.networkdemo.listener.OnLoadMoreListener;
import com.xdjk.networkdemo.net.DownloadCallBack;
import com.xdjk.networkdemo.net.NetLoadingDialog;
import com.xdjk.networkdemo.net.UploadOCallBack;
import com.xdjk.networkdemo.net.UserinfoServer;
import com.xdjk.networkdemo.net.bean.BaseResponse;
import com.xdjk.networkdemo.net.bean.WeatherData;
import com.xdjk.networkdemo.utils.ConstantUtils;
import com.xdjk.networkdemo.utils.NavigationController;
import com.xdjk.networkdemo.utils.ThemeUtile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import okhttp3.ResponseBody;

public class MainActivity extends BaseActivity implements OnItemClickLitener, OnLoadMoreListener {

    @BindView(R.id.mRecyclerview)
    XRecyclerView mRecyclerview;
    @BindView(R.id.null_data_layout)
    LinearLayout nullDataLayout;
    private ListAdapter mAdapter;
    private ArrayList<String> list = new ArrayList(Arrays.asList("获取天气数据", "动态baseUrl获取天气数据","图片上传","文件上传12","文件下载","滑动布局","滚动","模式切换","jeesite测试"));

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initParms(Bundle bundle) {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(layoutManager);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallBeat);
        mRecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mRecyclerview.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mRecyclerview.loadMoreComplete();
            }
        });
        mRecyclerview.setEmptyView(nullDataLayout);
        mAdapter = new ListAdapter(this, list);
        mRecyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickLitener(this);
        mAdapter.setOnLoadMoreListener(this);
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(MainActivity.this, list.get(position) + "", Toast.LENGTH_SHORT).show();
        if (0 == position) {
            UserinfoServer.getWeatherData("101010100", new NetLoadingDialog<WeatherData>(this) {
                @Override
                public void onResopnse(WeatherData weatherData) {
                    Logger.json(weatherData.toString());
                }

                @Override
                public void onFailure(Throwable e) {
                    Logger.d("onError:" + e.getMessage());
                }
            });
        } else if (1 == position) {
            UserinfoServer.getWeatherDataByUrl("101010100", new NetLoadingDialog<WeatherData>(this) {
                @Override
                public void onResopnse(WeatherData weatherData) {
                    Logger.json(weatherData.toString());
                }

                @Override
                public void onFailure(Throwable e) {
                    Logger.d("onError:" + e.getMessage());
                }
            });

        } else if (2 == position) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(granted -> {
                        if (granted) { // Always true pre-M
                            // I can control the camera now
                            Logger.d("已授权");

                            UserinfoServer.uploadUserIcon(Environment.getExternalStorageDirectory() + File.separator + "test/img.png", "测试", new UploadOCallBack<String>(this) {
                                @Override
                                public void onUpLoadSuccess(String s) {
                                    Logger.d("上传成功，s=" + s);
                                }

                                @Override
                                public void onUpLoadFail(Throwable e) {
                                    Logger.d("上传失败，e=" + e.getMessage());
                                }
                            });

                        } else {
                            // Oups permission denied
                            Logger.d("未授权");
                        }
                    });
        } else if (3 == position) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(granted -> {
                        if (granted) { // Always true pre-M
                            // I can control the camera now
                            Logger.d("已授权");

                            UserinfoServer.uploadFileWithRequestBody(Environment.getExternalStorageDirectory() + File.separator + "test/file2.apk", "测试",new UploadOCallBack<ResponseBody>(this) {

                                @Override
                                public void onUpLoadSuccess(ResponseBody s) {
                                    try {
                                        Logger.d("上传成功，s=" + s.string());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                        Logger.d("上传成功，解析异常,e=" + e.getMessage());
                                    }
                                }

                                @Override
                                public void onUpLoadFail(Throwable e) {
                                    Logger.e("上传失败，e=" + e.getMessage());
                                }

                            });

                        } else {
                            // Oups permission denied
                            Logger.d("未授权");
                        }
                    });
        } else if (4 == position) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(granted -> {
                        if (granted) { // Always true pre-M
                            // I can control the camera now
                            Logger.d("已授权");

                            UserinfoServer.fileDownload("UploadFileServer/FileManager.apk", new DownloadCallBack<ResponseBody>(this) {

                                @Override
                                public void onDownloadSucess() {
                                    Logger.d("文件下载成功 ");

                                }

                                @Override
                                public void onDownloadFail(Throwable e) {
                                    Logger.d("onError");
                                }
                            });

                        } else {
                            // Oups permission denied
                            Logger.d("未授权");
                        }
                    });
        } else if (5 == position) {
            NavigationController.getInstance().jumpTo(SlideActivity.class,null,false);
        } else if (6 == position) {
            NavigationController.getInstance().jumpTo(ScrollingActivity.class,null,false);
        } else if (7 == position) {
            if (ThemeUtile.isNight) {
                ThemeUtile.changeToTheme(this, ConstantUtils.THEME_LIGHT);
            } else {
                ThemeUtile.changeToTheme(this, ConstantUtils.THEME_DARK);
            }
        } else if (8 == position) {
            UserinfoServer.loginTest(new NetLoadingDialog<BaseResponse<Principal>>(this) {
                @Override
                public void onResopnse(BaseResponse<Principal> response) {
                    if (ConstantUtils.SUCESS.equals(response.code)) {
                        Logger.json(response.toString());
                    } else {
                        Logger.d("登陆失败：" + response.code + " -- " + response.msg);
                    }
                }

                @Override
                public void onFailure(Throwable e) {
                    Logger.d("登陆失败：" + e.getMessage());
                }
            });
        }

    }

    @Override
    public void onLoadMoreData() {

    }
}
